package cn.lyjuan.android.demofive.ui.webview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 2016/3/6.
 */
public class WebViewActivity extends Activity
{
    private WebView wv;

    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.setContentView(R.layout.ui_webview);

        wv = (WebView) this.findViewById(R.id.ui_webview);
        wv.addJavascriptInterface(this, "WebViewActivity");

        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl("file:///android_asset/ui_webview/001.html");
    }

    @JavascriptInterface
    public void say(String str)
    {
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }

    private void showSeachDialog()
    {
        LayoutInflater li = this.getLayoutInflater();
        View layout = li.inflate(R.layout.ui_webview_search, (ViewGroup) findViewById(R.id.ui_webview_search));

        final EditText text = (EditText) layout.findViewById(R.id.ui_webview_search_text);
        Button but = (Button) layout.findViewById(R.id.ui_webview_search_but);

        but.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String place = text.getText().toString();
                if (null == place || place.length() < 1)
                    return ;

                Log.i(WebViewActivity.class.getSimpleName(), "Search click : " + place);

                // 调用JS
                wv.loadUrl("javascript:search('" + text.getText() + "')");

                closeDialog();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);

        builder.setView(layout);
        dialog = builder.create();
        dialog.show();
    }

    private static final String ROUTE_BUS = "ROUTE_BUS";
    private static final String ROUTE_DRIVE = "ROUTE_DRIVE";

    private void showRouteDialog(final String routeType)
    {
        LayoutInflater li = this.getLayoutInflater();
        View layout = li.inflate(R.layout.ui_webview_route, (ViewGroup) this.findViewById(R.id.ui_webview_bus));

        final EditText fromEdit = (EditText) layout.findViewById(R.id.ui_webview_bus_from);
        final EditText toEdit = (EditText) layout.findViewById(R.id.ui_webview_bus_to);

        Button but = (Button) layout.findViewById(R.id.ui_webview_bus_but);

        but.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String from = fromEdit.getText().toString();
                String to = toEdit.getText().toString();
                if (from == null || from.length() < 1 || to == null || to.length() < 1)
                    return ;

                Log.i(WebViewActivity.class.getSimpleName(), "Route click : routeType >> " + routeType +
                        " from >> " + from + " to >> " + to);

                // 调用JS
                if (routeType.equals(ROUTE_BUS))
                    wv.loadUrl("javascript:routeBus('" + from + "', '" + to + "')");
                else if (routeType.equals(ROUTE_DRIVE))
                    wv.loadUrl("javascript:routeDrive('" + from + "', '" + to + "')");

                closeDialog();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);

        builder.setView(layout);
        dialog = builder.create();
        dialog.show();
    }

    /**
     * 路线规划返回
     * @param text      消息
     */
    @JavascriptInterface
    public void showDriveTimeDistance(String text)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);
        builder.setTitle("时间路程");

        builder.setMessage(text);

        dialog = builder.create();
        dialog.show();
    }

    public void dispatchDialog(View v)
    {
        switch (v.getId())
        {
            case R.id.ui_webview_but_search :
                showSeachDialog();
                break;
            case R.id.ui_webview_but_bus :
                showRouteDialog(ROUTE_BUS);
                break;
            case R.id.ui_webview_but_drive :
                showRouteDialog(ROUTE_DRIVE);
                break;
            default : break;
        }
    }

    private void closeDialog()
    {
        if (null != dialog)
            dialog.dismiss();
    }
}

